<?php
/**
 * PHP Social Networking
 * @author Michael Peacock
 * Registry Class
 */
class Registry {
	
	/**
	 * Array of objects
	 */
	private $objects;
	
	/**
	 * Array of settings
	 */
	private $settings;
	
    public function __construct() {
    }
    
    /**
     * Create a new object and store it in the registry
     * @param String $object the object file prefix
     * @param String $key pair for the object
     * @return void
     */
    public function createAndStoreObject( $object, $key )
    {
    	require_once( $object . '.class.php' );
    	$this->objects[ $key ] = new $object( $this );
    }
    
    /**
     * Get an object from the registries store
     * @param String $key the objects array key
     * @return Object
     */
    public function getObject( $key )
    {
    	return $this->objects[ $key ];
    }
    
    /**
     * Store Setting
     * @param String $setting the setting data
     * @param String $key the key pair for the settings array
     * @return void
     */
    public function storeSetting( $setting, $key )
    {
    	$this->settings[ $key ] = $setting;
    }
    
    /**
     * Get a setting from the registries store
     * @param String $key the settings array key
     * @return String the setting data
     */
    public function getSetting( $key )
    {
    	return $this->settings[ $key ];
    }
    
    public function errorPage( $heading, $content )
    {
    	$name = $this->getSetting('siteurl');
    	$content = $content."</br><b><a href='".$name."'>Back to login page</a></b>";
    	$this->getObject('template')->buildFromTemplates('main/error.tpl.php');
    	$this->getObject('template')->getPage()->addTag( 'heading', $heading );
    	$this->getObject('template')->getPage()->addTag( 'content', $content );
    }
    
    
    /**
     * Build a URL
     * @param array $urlBits bits of the array
     * @param array $queryString any query string data
     * @return String
     */
    public function buildURL( $urlBits, $queryString=array() )
    {
    	return $this->getObject('url')->buildURL( $urlBits, $queryString, false );
    }
    
    /**
     * Redirect the user to another location, displaying a message during the process
     * @param String $url the URL to redirect the user to
     * @param String $heading the message heading
     * @param String $message the message itself
     * @return void
     */
    public function redirectUser( $url)
    {
    	header("Location: {$url}");
    }
    
	function time_stamp($session_time)
	{

		$time_difference = time() - $session_time ;
		$seconds = $time_difference ;
		$minutes = round($time_difference / 60 );
		$hours = round($time_difference / 3600 );
		$days = round($time_difference / 86400 );
		$weeks = round($time_difference / 604800 );
		$months = round($time_difference / 2419200 );
		$years = round($time_difference / 29030400 );

		if($seconds <= 60)
		{
			return "$seconds seconds ago";
		}
		else if($minutes <=60)
		{
			if($minutes==1)
			{
				return "one minute ago";
			}
			else
			{
				return "$minutes minutes ago";
			}
		}
		else if($hours <=24)
		{
			if($hours==1)
			{
				return  "one hour ago";
			}
			else
			{
				return  "$hours hours ago";
			}
		}
		else if($days <=7)
		{
			if($days==1)
			{
				return "one day ago";
			}
			else
			{
				return  "$days days ago";
			}
		}
		else if($weeks <=4)
		{
			if($weeks==1)
			{
				return  "one week ago";
			}
			else
			{
				return  "$weeks weeks ago";
			}
		}
		else if($months <=12)
		{
			if($months==1)
			{
				return  "one month ago";
			}
			else
			{
				return  "$months months ago";
			}
		}
		else
		{
			if($years==1)
			{
				return  "one year ago";
			}
			else
			{
				return "$years years ago";
			}

		}
	}
    
}

?>